/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package supportlearning.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import supportlearning.entities.Account;

/**
 *
 * @author Huy
 */
@Stateless
public class login implements loginLocal {
    @PersistenceContext(unitName = "eprojectSupportLearning-ejbPU")
    private EntityManager em;
    
     public String Login(String UserName, String Password) {
        String result = "";
        List<Account> Listac = null;

        Query q = em.createNamedQuery("Account.Login");
        q.setParameter("username", UserName);
        q.setParameter("password", Password);

        Listac = q.getResultList();

        int roleid = 0;
        if (Listac.size() != 0) {
            for (int i = 0; i < Listac.size(); i++) {
                roleid = Listac.get(i).getCMSRole().getRoleId();
                if (roleid == 1) {
                    result = "Admin";
                    break;
                } else if (roleid == 2) {
                    result = "Staff";
                    break;
                } else if (roleid == 3) {
                    result = "Student";
                    break;
                }
            }
        } else {
            result = "UserName or password is fail.";
        }


        return result;
    }

    public void persist(Object object) {
        em.persist(object);
    }
}
